/**
 * Created by yangxianming on 2018/1/5.
 */

//
//接口定义 都是以api开头 所需参数都通过方法传入
//
//获取项目列表, 需要加载主页项目列表，新增项目弹框中的项目列表

//重置密码
function changePW(obj){
    var un = "username="+obj.getAttribute("id");

    var xmlHttp1 = new XMLHttpRequest();
    xmlHttp1.onreadystatechange = function(){
        if (xmlHttp1.readyState==4){
            if(xmlHttp1.status==200){
                var r = JSON.parse(xmlHttp1.response);
                if (r["apiStatus"] == "0") {
                    alert(r["errorMsg"])
                }
                if (r["apiStatus"] == "1"){
                    alert("密码重置成功:111111")
                }
            }
        }
    };
    xmlHttp1.open("GET", "/power/renewPW?"+un, true);
    xmlHttp1.send();
}

//查询用户权限
function apiGetUserInfo() {
    var xmlHttp1 = new XMLHttpRequest();
    xmlHttp1.onreadystatechange = function(){
        if (xmlHttp1.readyState==4){
            if(xmlHttp1.status==200){
                var r = JSON.parse(xmlHttp1.response);
                if (r["apiStatus"] == "0") {
                    alert(r["errorMsg"])
                }
                if (r["apiStatus"] == "1"){
                    drawUserInfo(r["result"]);
                }
            }
        }
    };
    xmlHttp1.open("get", "/power/userInfo", true);
    xmlHttp1.send();
}

//保存用户权限
function apiSetUserInfo(data){
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open("post", "/power/setUserInfo", true);
    xmlHttp.onreadystatechange = function(){
        if (xmlHttp.readyState==4){
            if (xmlHttp.status==200){
                var r = JSON.parse(xmlHttp.response);
                if (r["apiStatus"] == "1"){
                    alert(r["result"]);
                }else{
                    alert(r["errorMsg"]);
                }
            }
        }
    };
    xmlHttp.send(JSON.stringify(data));
}



//
//页面渲染 都是以draw开头
//
//从页面上拿取数据
function drawDataFromPage(obj){
    var userName = obj.getElementsByTagName("h1")[0].innerText;
    var apiList = document.getElementById(userName + "api").getElementsByTagName("div");
    var pageList = document.getElementById(userName + "page").getElementsByTagName("div");

    var apiPower = [];
    for(var a = 0; a < apiList.length; a++){
        var spanLine = apiList[a].getElementsByTagName("span");
        for(var s = 0; s < spanLine.length; s++){
            if(spanLine[s].getAttribute("hl") == "yes"){
                apiPower.push(spanLine[s].innerText);
            }
        }
    }

    var homePage = [];
    for(var h = 0; h < pageList.length; h++){
        var spanPage = pageList[h].getElementsByTagName("span");
        for(var sp = 0; sp < spanPage.length; sp++){
            if(spanPage[sp].getAttribute("hl") == "yes"){
                homePage.push(spanPage[sp].innerText);
            }
        }
    }
    var powerPage = {"apiPower": apiPower,"homePage": homePage};
    var data = {"userName": userName,"powerPage": powerPage};
    //console.log(data);
    apiSetUserInfo(data);
}

//加载全部权限数据,
function drawUserInfo(data) {
    var power = document.getElementById("powerList");
    power.innerText = "";
    var userPowerList = data["userPowerList"];
    var adminPower = data["adminPower"];

    for(var i = 0; i < userPowerList.length; i++){
        var d1 = document.createElement("div");
        var h1 = document.createElement("h1");
        var d2 = document.createElement("div");

        var userName = Object.keys(userPowerList[i])[0];
        d1.style.width = "100%";  //data-toggle="collapse" data-target="#c1"
        h1.setAttribute("data-toggle", "collapse");
        h1.setAttribute("data-target", "#"+userName);
        h1.innerText = userName;
        h1.setAttribute("aria-expanded", "false");
        h1.onclick = function(){drawsingleUserInfo(this)};
        d2.className = "collapse";
        d2.id = userName;
        d1.appendChild(h1);
        d1.appendChild(d2);
        power.appendChild(d1);

        var divB = document.createElement("div");
        divB.style.width = "100%";
        var button1 = document.createElement("button");
        button1.innerText = "重置密码";
        button1.style.float = "left";
        button1.onclick = function(){changePW(this.parentNode.parentNode)};
        var button2 = document.createElement("button");
        button2.innerText = "保存权限";
        button2.onclick = function () {drawDataFromPage(this.parentNode.parentNode.parentNode)};
        button2.style.float = "left";
        divB.appendChild(button1);
        divB.appendChild(button2);
        d2.appendChild(divB);

        //加载接口权限
        var oneUserReal = userPowerList[i][userName]["apiPower"];
        var oneUser = adminPower["apiPower"];
        var divh22 = document.createElement("div");
        divh22.style.width = "100%";
        var h22 = document.createElement("h2");
        h22.innerText = "功能权限";
        divh22.appendChild(h22);
        d2.appendChild(divh22);

        var divAllApi = document.createElement("div");
        divAllApi.id = userName + "api";
        for(var m = 0; m < oneUser.length; m++){
            var divLineApi = document.createElement("div");
            divLineApi.ondblclick =  new Function("return false");
            for(var n = 0; n < oneUser[m].length; n++){
                var span2 = document.createElement("span");
                span2.innerText = oneUser[m][n];
                span2.ondblclick =  new Function("return false");
                if(n == 0){
                    span2.style.fontWeight = "bold";
                }
                if(oneUserReal.indexOf(oneUser[m][n]) == -1){
                    span2.setAttribute("hl", "no");
                }else{
                    span2.setAttribute("hl", "yes");
                    span2.style.backgroundColor = "#47dc67";
                }
                //if(typeof(oneUserReal[m][n]) == "undefined"){
                //    //undefined这个在新增接口的时候普通用户将会没有这个权限
                //    span2.setAttribute("hl", "no");
                //}else{
                //    if(oneUserReal[m][n].indexOf("x;") == -1){
                //        span2.setAttribute("hl", "yes");
                //        span2.style.backgroundColor = "#47dc67";
                //    }else{
                //        span2.setAttribute("hl", "no");
                //    }
                //}


                span2.onclick = function(){drawSetColor(this)};
                divLineApi.appendChild(span2);
            }
            divAllApi.appendChild(divLineApi);
            divAllApi.appendChild(document.createElement("br"));
        }
        d2.appendChild(divAllApi);

        //加载左侧权限
        var userHomePage = Object.values(userPowerList[i])[0]["homePage"];
        var adminHomePage = adminPower["homePage"];
        var h21 = document.createElement("h2");
        h21.innerText = "左侧权限";
        var divh21 = document.createElement("div");
        divh21.style.width = "100%";
        divh21.appendChild(h21);
        d2.appendChild(divh21);

        var divAllLeft = document.createElement("div");
        divAllLeft.id = userName + "page";
        for(var x = 0; x < adminHomePage.length; x++){
            var divLine = document.createElement("div");
            divLine.ondblclick =  new Function("return false");
            for(var y = 0; y < adminHomePage[x].length; y++){
                var span = document.createElement("span");
                span.innerText = adminHomePage[x][y];
                span.ondblclick =  new Function("return false");
                if(y == 0){
                    span.style.fontWeight = "bold";
                }

                if(userHomePage.indexOf(adminHomePage[x][y]) == -1){
                    span.setAttribute("hl", "no");
                }else{
                    span.setAttribute("hl", "yes");
                    span.style.backgroundColor = "#47dc67";
                }
                //if(typeof(userHomePage[x][y]) == "undefined"){
                //    span.setAttribute("hl", "no");
                //}else{
                //    if(userHomePage[x][y].indexOf("x;") == -1){
                //        span.setAttribute("hl", "yes");
                //        span.style.backgroundColor = "#47dc67";
                //    }else{
                //        span.setAttribute("hl", "no");
                //    }
                //}

                span.onclick = function(){drawSetColor(this)};
                divLine.appendChild(span);
            }
            divAllLeft.appendChild(divLine);
            divAllLeft.appendChild(document.createElement("br"));
        }
        d2.appendChild(divAllLeft);

    }
}



//加载用户权限数据
function drawsingleUserInfo(obj){
    if(obj.getAttribute("aria-expanded") == "false"){
    }
}

//权限点击高亮，再点击透明
function drawSetColor(ob){
    if(ob.getAttribute("hl") == "yes"){
        ob.style.backgroundColor = "transparent";
        ob.setAttribute("hl", "no");
        return;
    }
    if(ob.getAttribute("hl") == "no"){
        ob.style.backgroundColor = "#47dc67";
        ob.setAttribute("hl", "yes");
    }
}

//
//页面调用 数据获取，接口层与渲染层的连接
//
//初始化数据 加载主页面项目列表，加载全部用例列表
function initializeData(){
    apiGetUserInfo();
}


